<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .grid{
            margin: auto;
            width: 500px;
            text-align: center;
        }
        table{
            width: 100%;
            border-collapse: collapse;
        }
        th,td{
            padding: 10px;
            border:1px dashed orange;
            height: 35px;
            line-height: 35px;
        }
        th{
            background-color: orange;
        }
        .book{
            margin-bottom: 10px;
        }
        .total{
            height: 30px;
            line-height: 30px;
            background-color: orange;
            border: 1px solid #ccc;
        }
    </style>
</head>
<body>
    <div id="app">
        <div class="grid">
            <div>
                <h1>图书管理</h1>
                <div class="book">
                    <div>
                        <label for="id">
                            编号:
                        </label>
                        <input type="text" id="id" v-model='id' :disabled='flag'>
                       <label for="name" >
                            名称:
                       </label>
                       <input type="text"  id="name" v-model='name'>
                        <button @click='handle' :disabled='submitFlag'>提交</button>
                    </div>
                </div>
                <div class="total">
                    <span>图书总数：</span>
                    <span>{{total}}</span>
                </div>
            </div>
            <table>
                <thead>
                    <tr>
                        <th>编号</th>
                        <th>名称</th>
                        <th>时间</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody>
                    <tr v-for='item in books' :key='item.id'>
                        <td>{{item.id}}</td>
                        <td>{{item.name}}</td>
                        <td>{{item.date | dateformat }}</td>
                        <td>
                            <a href="" @click.prevent = 'modify(item.id)'>修改</a>
                            <span>|</span>
                            <a href="" @click.prevent = 'delet(item.id)'>删除</a>
                        </td>
                    </tr>
                    
                </tbody>
            </table>
        </div>
    </div>
    <script src="../node_modules/vue/dist/vue.js"></script>
    <script src="./moment.js"></script>
    <script>
        
        // Vue.filter('dateformat',function(val){
            
        //     return  moment(parseInt(val)).format('YYYY-MM-DD');
        // })
        var vm = new Vue({
            el:'#app',
            data:{
                id:'',
                name:'',
                flag:false,
                submitFlag:false,
                books:[]
            },
            methods: {
               
                handle(){
                    if(this.flag){
                        // 在编辑 根据当前id更新数组中对应数据
                        this.books.some(item=>{
                            // 箭头函数的this指的是父级作用域的this
                            if(item.id == this.id){
                                item.name = this.name;
                                return true;
                            }
                        })
                        this.flag = false;
                    }else{
                        // 添加图书
                        var book = {};
                        book.id = this.id;
                        book.name = this.name;
                        book.date = new Date().getTime();

                        this.books.push(book);
                    }
                    
                   
                    //  清空表单
                     this.id = this.name = '';
                },
                modify(id){
                    // 修改图书
                    var book = this.books.filter(item=>{
                        return item.id == id
                    })
                    this.id = book[0].id;
                    this.name = book[0].name;
                    // 禁止修改id
                    this.flag = true;

                },
                delet(id){
                    // 删除图书
                    // 查找符合条件的索引
                    var index = this.books.findIndex(item=>{
                        return item.id == id;
                    });
                    this.books.splice(index,1);
                }
            },
            filters:{
                dateformat: function(val){
                    return  moment(parseInt(val)).format('YYYY-MM-DD');
                }
            },
            computed:{
                total:function(){
                    // 计算图书总数
                    return this.books.length;
                }
            },
            // 主要监听异步的一些逻辑
            watch:{
                name:function(val){
                    // 验证图书名称是否已经存在
                    var flag = this.books.some(item=>{
                        return item.name == val;
                    });
                    if(flag){
                        this.submitFlag = true;
                    }else{
                        this.submitFlag = false;
                    }
                }
            },
            mounted() {
                // 模板已经可以使用了
                // 可以从后台获取数据，将数据填充到模板中
                var data = [
                    {
                        id: 1,
                        name: '三国演义',
                        date: '2525609975000'
                    }, {
                        id: 2,
                        name: '水浒传',
                        date: '2525609975000'
                    }, {
                        id: 3,
                        name: '红楼梦',
                        date: '2525609975000'
                    }, {
                        id: 4,
                        name: '西游记',
                        date: '2525609975000'
                    }
                ];
                this.books = data;
            },
            
        })
       
    </script>
</body>
</html>